package com.ruoyi.project.system.banner.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.banner.domain.Banner;
import com.ruoyi.project.system.banner.dto.BannerDto;
import com.ruoyi.project.system.banner.service.IBannerService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.file.domain.UploadFile;
import com.ruoyi.project.system.file.service.IUploadFileService;
import com.ruoyi.project.system.goods.goods.domain.GoodsGoods;
import com.ruoyi.project.system.goods.goods.enums.GoodsStatus;
import com.ruoyi.project.system.goods.goods.service.IGoodsGoodsService;

/**
 * 轮播图Controller
 * 
 * @author LCL
 * @date 2020-06-05
 */
@Controller
@RequestMapping("/system/banner")
public class BannerController extends BaseController
{
    private String prefix = "system/banner";

    @Autowired
    private IBannerService bannerService;

    @Autowired
	private IUploadFileService uploadFileService;

    @Autowired
    private IGoodsGoodsService goodsGoodsService;

    @RequiresPermissions("system:banner:view")
    @GetMapping()
    public String banner()
    {
        return prefix + "/banner";
    }

    /**
     * 查询轮播图列表
     */
    @RequiresPermissions("system:banner:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Banner banner)
    {
        startPage();
        List<Banner> list = bannerService.selectBannerList(banner);
        return getDataTable(list);
    }

    /**
     * 导出轮播图列表
     */
    @RequiresPermissions("system:banner:export")
    @Log(title = "轮播图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Banner banner)
    {
        List<Banner> list = bannerService.selectBannerList(banner);
        ExcelUtil<Banner> util = new ExcelUtil<Banner>(Banner.class);
        return util.exportExcel(list, "banner");
    }

    /**
     * 新增轮播图
     */
    @GetMapping("/add")
    public String add(ModelMap map)
    {
        GoodsGoods goodsGoods = new GoodsGoods();
        goodsGoods.setStatus(GoodsStatus.UP_SHELF.getValue());
        map.put("goodsList",this.goodsGoodsService.selectGoodsGoodsList(goodsGoods));
        return prefix + "/add";
    }

    /**
     * 新增保存轮播图
     */
    @RequiresPermissions("system:banner:add")
    @Log(title = "轮播图", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public AjaxResult addSave(Banner banner)
    {
        if(banner.getType()!=1){
            String[] res = banner.getResId().split(",");
            if(banner.getType()==0){
                banner.setResId(res[0]);
            }else if(banner.getType()==2){
                banner.setResId(res[1]);
            }
        }else{
            banner.setResId(null);
        }
        bannerService.insertBanner(banner);
        //通过图片路径修改附件表中的相关资源数据
//        this.uploadFileService.updateUploadFileByRealPaths(Arrays.asList(banner.getHeadRealUrl(),banner.getBackgroundRealUrl()),new UploadFile(null,null,null,banner.getId()));
        return AjaxResult.success();
    }

    /**
     * 修改轮播图
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        BannerDto banner = bannerService.selectBannerById(id);
        mmap.put("banner", banner);
        GoodsGoods goodsGoods = new GoodsGoods();
        goodsGoods.setStatus(GoodsStatus.UP_SHELF.getValue());
        mmap.put("goodsList",this.goodsGoodsService.selectGoodsGoodsList(goodsGoods));
        return prefix + "/edit";
    }

    /**
     * 修改保存轮播图
     */
    @RequiresPermissions("system:banner:edit")
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @Transactional
    public AjaxResult editSave(Banner banner)
    {
        //查询数据库获取旧数据
        BannerDto oldBanner = bannerService.selectBannerById(banner.getId());
        //判断数据是否存在
        if(oldBanner==null){
            return AjaxResult.error("数据不存在");
        }
        if(banner.getType()!=1){
            String[] res = banner.getResId().split(",");
            if(banner.getType()==0){
                banner.setResId(res[0]);
            }else if(banner.getType()==2){
                banner.setResId(res[1]);
            }
        }else{
            banner.setResId(null);
        }
        //更新数据
        bannerService.updateBanner(banner);

//        //删除的file集合
//        List delRealPaths = new ArrayList<String>();
//
//        //新增的file集合
//		List addRealPaths = new ArrayList<String>();
//
//        //判断1.是否是新增图片 2.是否修改背景图片
//        if(oldBanner.getHeadUrl()==null){
//            addRealPaths.add(banner.getHeadUrl());
//        }else if(!oldBanner.getHeadUrl().equals(banner.getHeadUrl())){
//            //删除原本存在的旧图
//            FileUploadUtils.delete(oldBanner.getHeadRealUrl());
//
//            delRealPaths.add(oldBanner.getHeadRealUrl());
//
//			addRealPaths.add(banner.getHeadRealUrl());
//        }
//        //判断1.是否是新增图片 2.是否修改背景图片
//        if(oldBanner.getBackgroundUrl()==null){
//            addRealPaths.add(banner.getBackgroundRealUrl());
//        }else if(!oldBanner.getBackgroundUrl().equals(banner.getBackgroundUrl())){
//            //删除原本存在的旧图
//            FileUploadUtils.delete(oldBanner.getBackgroundRealUrl());
//
//			delRealPaths.add(oldBanner.getBackgroundRealUrl());
//
//			addRealPaths.add(banner.getBackgroundRealUrl());
//        }
//
//		if(delRealPaths.size()!=0){
//			//通过图片路径删除附件表中的数据
//			this.uploadFileService.deleteUploadFileByRealPaths(delRealPaths);
//		}
//		if(addRealPaths.size()!=0){
//            //通过图片路径修改附件表中的相关资源数据
//            this.uploadFileService.updateUploadFileByRealPaths(addRealPaths,new UploadFile(null,null,null,banner.getId()));
//        }

        return AjaxResult.success();
    }

    /**
     * 删除轮播图
     */
    @RequiresPermissions("system:banner:remove")
    @Log(title = "轮播图", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    @Transactional
    public AjaxResult remove(String ids)
    {
        //查询数据库中旧数据
        List<Banner> bannerList = this.bannerService.selectBannerByIds(ids);
        //删除数据
        bannerService.deleteBannerByIds(ids);
//        for(Banner banner:bannerList){
//            //删除原本的旧图
//            FileUploadUtils.delete(banner.getBackgroundRealUrl());
//            FileUploadUtils.delete(banner.getHeadRealUrl());
//            //删除资源文件表中的数据
//			this.uploadFileService.deleteUploadFileByRealPaths(Arrays.asList(banner.getBackgroundRealUrl(),banner.getHeadRealUrl()));
//        }
        return AjaxResult.success();
    }
}
